
/**
|--------------------------------------------------
| Constants
|--------------------------------------------------
*/
export const LOGIN_ING = 'LOGIN_ING'
export const LOGIN = 'LOGIN'
export const LOGIN_SUC = 'LOGIN_SUC'
export const LOGIN_FAIL = 'LOGIN_FAIL'
/**
|--------------------------------------------------
| Actions
|--------------------------------------------------
*/
export function login(data) {
    return {
        type: LOGIN,
        data
    }
}

/**
|--------------------------------------------------
| Reducer
|--------------------------------------------------
*/

const initialState = {
    logged: localStorage.getItem('logged') != null && localStorage.getItem('logged') != 'false',
    loading: false
}

export default function (state = initialState, action) {
    switch (action.type) {
        case LOGIN_ING:
            return { ...state, loading: true };
        case LOGIN_SUC:
            return { ...state, loading: false, logged: true, userInfo: action.userInfo, token: action.token }
        case LOGIN_FAIL:
            return { ...state, loading: false, logged: false, userInfo: null, token: '' };
        default:
            return initialState
    }
}